﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using OpenComposite.EII.Repository;
using OpenComposite.Base;

namespace OpenComposite.EII.Forms.OrgDesigner
{
	public partial class UsersDock : DockContent
	{
		public UsersDock()
		{
			InitializeComponent();
		}

		public ListView ListView
		{
			get { return lvUsers; }
		}
		
		private void filterPersons(string p)
		{
			if ( FilterPersons != null ) {
				FilterPersons(this, new StringEventArgs(p));
			}
		}

		public event StringEventHandler FilterPersons;

		/// <summary>
		/// Refreshes the item with the specified user id.
		/// </summary>
		/// <param name="userId">The user id.</param>
		public void Refresh(int userId)
		{
			if ( lvUsers.SelectedItems.Count != 0 ) {
				User user = lvUsers.SelectedItems[0].Tag as User;
				if ( user != null && user.ID == userId ) {
					lvUsers.SelectedItems[0].Text = user.FormattedName;
					return;
				}
			}
			foreach ( ListViewItem item in lvUsers.Items ) {
				User user = item.Tag as User;
				if ( user != null && user.ID == userId ) {
					item.Text = user.FormattedName;
				}
			}
		}

		#region Filter Links

		private void linkShowAll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			filterPersons(null);
		}

		private void linkLabelZ_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			filterPersons("z");
		}

		private void linkLabelY_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			filterPersons("y");
		}

		private void linkLabelX_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			filterPersons("x");
		}

		private void linkLabelW_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			filterPersons("w");
		}

		private void linkLabelV_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			filterPersons("v");
		}

		private void linkLabelU_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			filterPersons("u");
		}

		private void linkLabelT_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			filterPersons("t");

		}

		private void linkLabelS_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			filterPersons("s");
		}

		private void linkLabelR_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			filterPersons("r");
		}

		private void linkLabelQ_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			filterPersons("q");
		}

		private void linkLabelP_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			filterPersons("p");
		}

		private void linkLabelO_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			filterPersons("o");
		}

		private void linkLabelN_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			filterPersons("n");
		}

		private void linkLabelM_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			filterPersons("m");
		}

		private void linkLabelL_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			filterPersons("l");
		}

		private void linkLabelK_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			filterPersons("k");
		}

		private void linkLabelJ_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			filterPersons("j");
		}

		private void linkLabelI_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			filterPersons("i");
		}

		private void linkLabelH_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			filterPersons("h");
		}

		private void linkLabelG_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			filterPersons("g");
		}

		private void linkLabelF_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			filterPersons("f");
		}

		private void linkLabelE_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			filterPersons("e");
		}

		private void linkLabelD_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			filterPersons("d");
		}

		private void linkLabelC_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			filterPersons("c");
		}

		private void linkLabelB_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			filterPersons("b");
		}

		private void linkLabelA_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			filterPersons("a");
		}
		#endregion

		private void lvUsers_ItemDrag(object sender, ItemDragEventArgs e)
		{
			lvUsers.DoDragDrop(e.Item, DragDropEffects.Link);
		}

		private void lvUsers_KeyUp(object sender, KeyEventArgs e)
		{
			if ( e.KeyCode == Keys.I && e.Alt ) {
				if ( lvUsers.SelectedItems.Count == 0 ) return;
				Helpers.OpenDebugViewer(lvUsers.SelectedItems[0].Tag as RepositoryItemBase);
			}
		}
	}
}
